﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace NSATBPortal
{
    public partial class DefRunItemDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string sOp = Request.QueryString["Op"].ToString();
                string sParentName = Server.UrlDecode(Request.QueryString["ParentName"].ToString());

                lblParent.Text = "Add Item " + " for " + sParentName;
                cmdSave.Text = "Add";

                string sVal = Request.QueryString["ParentID"].ToString();
                int iParentID = Convert.ToInt32(sVal);

                using (ATBSchemaDB objATBDB = new ATBSchemaDB())
                {
                    DataTable dtList;

                    dtList = objATBDB.GetDefTestSetList();
                    dropdownTestSet.DataValueField = "ID";
                    dropdownTestSet.DataTextField = "Name";
                    dropdownTestSet.DataSource = dtList;
                    dropdownTestSet.DataBind();
                    dropdownTestSet.Items.Add(new ListItem("[ Select ]", "0"));
                    dropdownTestSet.SelectedValue = "0";

                    dtList = objATBDB.GetDefBaseStateList();
                    dropdownBaseState.DataValueField = "ID";
                    dropdownBaseState.DataTextField = "Name";
                    dropdownBaseState.DataSource = dtList;
                    dropdownBaseState.DataBind();
                    dropdownBaseState.Items.Add(new ListItem("[ Select ]", "0"));
                    dropdownBaseState.SelectedValue = "0";

                    dtList = objATBDB.GetTestMachineGroupList();
                    dropdownTestMachineGroup.DataValueField = "ID";
                    dropdownTestMachineGroup.DataTextField = "Name";
                    dropdownTestMachineGroup.DataSource = dtList;
                    dropdownTestMachineGroup.DataBind();
                    dropdownTestMachineGroup.Items.Add(new ListItem("[ Select ]", "0"));
                    dropdownTestMachineGroup.SelectedValue = "0";
                }
            }
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string sOp = Request.QueryString["Op"].ToString();
            string sUserID = Request.LogonUserIdentity.Name;
            int iParentID = Convert.ToInt32(Server.UrlDecode(Request.QueryString["ParentID"].ToString()));
            if (dropdownTestSet.SelectedValue == "0")
            {
                lblErrorMessage.Text = "ERROR: You must pick a value for" + lblTestSet.Text ;
                return;
            }
            if (dropdownBaseState.SelectedValue == "0")
            {
                lblErrorMessage.Text = "ERROR: You must pick a value for" + lblBaseState.Text ;
                return;
            }
            if (dropdownTestMachineGroup.SelectedValue == "0")
            {
                lblErrorMessage.Text = "ERROR: You must pick a value for" + lblTestMachineGroup.Text ;
                return;
            }

            using (ATBSchemaDB objATBDB = new ATBSchemaDB())
            {
                objATBDB.CreateDefRunItem
                    (  
                        iParentID,
                        Convert.ToInt32(dropdownTestSet.SelectedValue),        
                        Convert.ToInt32(dropdownBaseState.SelectedValue),        
                        Convert.ToInt32(dropdownTestMachineGroup.SelectedValue),
                        sUserID)
                    ;
            }

            Response.Redirect("DefRunEditItems.aspx?ID=" + Request.QueryString["ParentID"].ToString() + "&Name=" + Request.QueryString["ParentName"].ToString());
            
        }

        protected void cmdCancel_Click(object sender, EventArgs e)
        {

            Response.Redirect("DefRunEditItems.aspx?ID=" + Request.QueryString["ParentID"].ToString() + "&Name=" + Request.QueryString["ParentName"].ToString());

        }
    }
}